//
//  CheckMoreCell.m
//  HappyBaby
//
//  Created by lanou on 15/11/26.
//  Copyright © 2015年 华哥. All rights reserved.
//

#import "CheckMoreCell.h"
#import "Define.h"
#import "UIImageView+WebCache.h"

@interface CheckMoreCell ()

@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UILabel *collect;
@property (weak, nonatomic) IBOutlet UILabel *category;
@property (weak, nonatomic) IBOutlet UILabel *postAuthor;



@property (strong, nonatomic)  UILabel *postTitle;
@property (strong, nonatomic)  UILabel *postExcerpt;
@property (strong, nonatomic)  UILabel *postTag1;
@property (strong, nonatomic)  UILabel *postTag2;
@property (strong, nonatomic)  UILabel *postTag3;
@end

@implementation CheckMoreCell



- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
    if (self) {
        
        // 初始化时加载collectionViewCell.Xib文件
        NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"CheckMoreCell" owner:self options:nil];
        /*
        // 如果路径不存在 直接返回nil
        if (arrayOfViews.count < 1) {
            return nil;
        }
        
      //下面两个判断是为防止出错而写的，，如果说使我们自己拖入的控件，清楚，可以不写
        // 如果xib中的View不属于UICollectionViewCell类。return nil
        if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UITableViewCell class]]) {
            return nil;
        }
        */
        // 加载nib
        self = [arrayOfViews objectAtIndex:0];
        self.iconImageView.layer.cornerRadius = 10;
        self.iconImageView.layer.masksToBounds = YES;
        
        // 标题
        self.postTitle = [[UILabel alloc] initWithFrame:CGRectMake(16*kScaleX, 15*kScaleY, 211*kScaleX, 40*kScaleY)];
        self.postTitle.numberOfLines = 0;
        self.postTitle.font = [UIFont boldSystemFontOfSize:kScreenWidth/20];
        [self.contentView addSubview:_postTitle];
        
        // 摘要
        self.postExcerpt = [[UILabel alloc] initWithFrame:CGRectMake(16*kScaleX, 102*kScaleY, 223*kScaleX, 25*kScaleY)];
        self.postExcerpt.textColor = [UIColor grayColor];
        self.postExcerpt.numberOfLines = 0;
        self.postExcerpt.font = [UIFont boldSystemFontOfSize:kScreenWidth/25];
        [self.contentView addSubview:_postExcerpt];
        
        // tag1
        self.postTag1 = [[UILabel alloc] initWithFrame:CGRectMake(16*kScaleX, 165*kScaleY, 58*kScaleX, 25*kScaleY)];
        self.postTag1.textAlignment = NSTextAlignmentCenter;
        self.postTag1.layer.cornerRadius = 5;
        self.postTag1.layer.masksToBounds = YES;
        self.postTag1.layer.borderWidth = 2;
        self.postTag1.layer.borderColor = [UIColor colorWithRed:163/255.0 green:220/255.0 blue:223/255.0 alpha:1].CGColor;
        self.postTag1.backgroundColor = [UIColor colorWithRed:229/255.0 green:247/255.0 blue:246/255.0 alpha:1];
        self.postTag1.textColor= [UIColor darkGrayColor];
        self.postTag1.font = [UIFont systemFontOfSize:kScreenWidth/30];
        [self.contentView addSubview:_postTag1];
        
        // tag2
        self.postTag2 = [[UILabel alloc] initWithFrame:CGRectMake(self.postTag1.x + self.postTag1.width, 165*kScaleY, 58*kScaleX, 25*kScaleY)];
        self.postTag2.textAlignment = NSTextAlignmentCenter;
        self.postTag2.layer.cornerRadius = 5;
        self.postTag2.layer.masksToBounds = YES;
        self.postTag2.layer.borderWidth = 2;
        self.postTag2.layer.borderColor = [UIColor colorWithRed:243/255.0 green:203/255.0 blue:158/255.0 alpha:1].CGColor;
        self.postTag2.backgroundColor = [UIColor colorWithRed:247/255.0 green:241/255.0 blue:233/255.0 alpha:1];
        self.postTag2.textColor = [UIColor darkGrayColor];
        self.postTag2.font = [UIFont systemFontOfSize:kScreenWidth/30];
        [self.contentView addSubview:_postTag2];
        
        // tag3
        self.postTag3 = [[UILabel alloc] initWithFrame:CGRectMake(self.postTag2.x + self.postTag2.width, 165*kScaleY, 58*kScaleX, 25*kScaleY)];
        self.postTag3.textAlignment = NSTextAlignmentCenter;
        self.postTag3.layer.cornerRadius = 5;
        self.postTag3.layer.masksToBounds = YES;
        self.postTag3.layer.borderWidth = 2;
        self.postTag3.layer.borderColor = [UIColor colorWithRed:233/255.0 green:174/255.0 blue:178/255.0 alpha:1].CGColor;
        self.postTag3.backgroundColor = [UIColor colorWithRed:249/255.0 green:231/255.0 blue:235/255.0 alpha:1];
        self.postTag3.textColor = [UIColor darkGrayColor];
        self.postTag3.font = [UIFont systemFontOfSize:kScreenWidth/30];
        [self.contentView addSubview:_postTag3];
        
        
    }
    return self;
}


- (void)setModel:(CheckMoreModel *)model
{
    _model = model;
    self.postTitle.text = model.post_title;
    self.category.text = [model.post_category substringToIndex:2];
    // 根据分类名修改label的颜色
    [self changeCategoryLableNameWithText:self.category.text];
    
    self.postAuthor.text = model.post_author;
    self.postExcerpt.text = model.post_excerpt;
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.post_cover_image] placeholderImage:nil];
    self.collect.text = [NSString stringWithFormat:@"收藏%ld",model.post_collect_count];
    NSArray *tagsArray = model.post_tags;
    NSMutableArray *tagStringArr = [NSMutableArray array];
    
    // 给下面三个label赋值
    for (NSDictionary *dic in tagsArray) {
        NSString *string =[NSString stringWithFormat:@"%@",(NSString *)[dic objectForKey:@"tag"]];
        [tagStringArr addObject:string];
    }
    self.postTag1.text = tagStringArr[0];
    self.postTag2.text = tagStringArr[1];
    self.postTag3.text = tagStringArr[2];
    // 修改动态的frame
    [self changeLabelFrame];
}

- (void)changeLabelFrame
{
    // 获取postTitle动态高度
    CGSize titleSize = [self stringSizeWithFont:[UIFont boldSystemFontOfSize:kScreenWidth/20] string:self.postTitle.text width:self.postTitle.width height:100];
    self.postTitle.height = titleSize.height;
    
    // 获取postExcerpt动态高度
    CGSize excerptSize = [self stringSizeWithFont:[UIFont boldSystemFontOfSize:kScreenWidth/25] string:self.postExcerpt.text width:self.postExcerpt.width height:200];
    self.postExcerpt.height = excerptSize.height;
    
    //    // 获取tagLabel的动态宽度
    CGSize tag1Size = [self stringSizeWithFont:[UIFont systemFontOfSize:kScreenWidth/30] string:self.postTag1.text width:100 height:self.postTag1.height];
    self.postTag1.width = tag1Size.width + 20*kScaleX;
    self.postTag2.x = self.postTag1.x + self.postTag1.width + 10 * kScaleX;
    
    CGSize tag2Size = [self stringSizeWithFont:[UIFont systemFontOfSize:kScreenWidth/30] string:self.postTag2.text width:100 height:self.postTag2.height];
    self.postTag2.width = tag2Size.width + 20*kScaleX;
    self.postTag3.x = self.postTag2.x + self.postTag2.width + 10 * kScaleX;
    
    CGSize tag3Size = [self stringSizeWithFont:[UIFont systemFontOfSize:kScreenWidth / 30] string:self.postTag3.text width:100 height:self.postTag3.height];
    self.postTag3.width = tag3Size.width + 20*kScaleX;
}

- (void)changeCategoryLableNameWithText:(NSString *)text
{
    if ([text isEqualToString:@"绘本"]) {
        self.category.backgroundColor = [UIColor colorWithRed:71/225.0 green:168/225.0 blue:254/225.0 alpha:1];
        self.category.text = @"童书";
    }else if ([text isEqualToString:@"游戏"]){
        self.category.backgroundColor = [UIColor colorWithRed:236/225.0 green:116/225.0 blue:176/225.0 alpha:1];
    }else if ([text isEqualToString:@"玩具"]){
        self.category.backgroundColor = [UIColor colorWithRed:55/225.0 green:169/225.0 blue:131/225.0 alpha:1];
    }
}

// 根据字体和字符串来求文字的高度
- (CGSize)stringSizeWithFont:(UIFont *)font string:(NSString *)string width:(CGFloat)width height:(CGFloat)height{
    CGRect rect = [string boundingRectWithSize:CGSizeMake(width, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil];
    
    return rect.size;
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
